import java.io.*;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.InputMismatchException;
import java.util.Queue;

/**
 * @author Egor Kulikov (egor@egork.net)
 */
public class Moon {
	@SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
	InputReader in;
	PrintWriter out;
	private int[] dRow = {1, 0, -1, 0};
	private int[] dColumn = {0, -1, 0, 1};

	void solve() {
		int columnCount = in.readInt();
		int rowCount = in.readInt();
		char[][] map = new char[rowCount][];
		for (int i = 0; i < rowCount; i++)
			map[i] = in.readString().toCharArray();
		@SuppressWarnings({"unchecked"})
		Queue<Integer>[] queue = new Queue[4];
		boolean[][][] visited = new boolean[rowCount][columnCount][4];
		for (int i = 0; i < 4; i++)
			queue[i] = new ArrayDeque<Integer>();
		for (int i = 0; i < rowCount; i++) {
			for (int j = 0; j < columnCount; j++) {
				if (map[i][j] == 'S') {
					queue[0].add(i * columnCount + j);
					visited[i][j][0] = true;
				}
			}
		}
		while (true) {
			boolean updated = false;
			for (int i = 0; i < 4; i++) {
				if (queue[i].isEmpty())
					continue;
				updated = true;
				int row = queue[i].peek() / columnCount;
				int column = queue[i].poll() % columnCount;
				for (int j = 0; j < 4; j++) {
					int newRow = row + dRow[j];
					int newColumn = column + dColumn[j];
					if (newRow < 0 || newRow >= rowCount || newColumn < 0 || newColumn >= columnCount)
						continue;
					int energy = i;
					if (map[newRow][newColumn] == 'R')
						energy = 0;
					else if (map[newRow][newColumn] == 'D')
						energy++;
					if (energy > 3 || visited[newRow][newColumn][energy])
						continue;
					for (int k = energy; k < 4; k++)
						visited[newRow][newColumn][k] = true;
					queue[energy].add(newRow * columnCount + newColumn);
				}
			}
			if (!updated)
				break;
		}
		int reachable = 0;
		for (int i = 0; i < rowCount; i++) {
			for (int j = 0; j < columnCount; j++) {
				if (map[i][j] == 'G' && visited[i][j][3])
					reachable++;
			}
		}
		out.println(reachable);
	}

	public static void main(String[] args) {
		new Moon().run();
	}

	Moon() {
		@SuppressWarnings({"UnusedDeclaration"})
		String id = getClass().getName().toLowerCase();
		//noinspection EmptyTryBlock
		try {
			System.setIn(new FileInputStream(id + ".in"));
			System.setOut(new PrintStream(new FileOutputStream(id + ".out")));
//			System.setIn(new FileInputStream("input.txt"));
//			System.setOut(new PrintStream(new FileOutputStream("output.txt")));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		in = new InputReader(System.in);
		out = new PrintWriter(System.out);
	}

	void run() {
		//noinspection InfiniteLoopStatement
		solve();
		exit();
	}

	@SuppressWarnings({"UnusedDeclaration"})
	void exit() {
		out.close();
		System.exit(0);
	}

	@SuppressWarnings({"UnusedDeclaration"})
	static class InputReader {
		InputStream stream;
		byte[] buf = new byte[1024];
		int curChar, numChars;

		InputReader(InputStream stream) {
			this.stream = stream;
		}

		int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		int readInt() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		long readLong() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		String readString() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuffer res = new StringBuffer();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		String readLine0() {
			StringBuffer buf = new StringBuffer();
			int c = read();
			while (c != '\n' && c != -1) {
				if (c != '\r')
					buf.appendCodePoint(c);
				c = read();
			}
			return buf.toString();
		}

		String readLine() {
			String s = readLine0();
			while (s.trim().length() == 0)
				s = readLine0();
			return s;
		}

		String readLine(boolean ignoreEmptyLines) {
			if (ignoreEmptyLines)
				return readLine();
			else
				return readLine0();
		}

		BigInteger readBigInteger() {
			try {
				return new BigInteger(readString());
			} catch (NumberFormatException e) {
				throw new InputMismatchException();
			}
		}

		char readCharacter() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			return (char) c;
		}

		double readDouble() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.') {
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, readInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.') {
				c = read();
				double m = 1;
				while (!isSpaceChar(c)) {
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, readInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}
	}
}
